package com.liaowei.novel.fragment;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.liaowei.novel.R;
import com.liaowei.novel.activity.NovelListActivity;
import com.liaowei.novel.domain.Category;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link CategoryFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class CategoryFragment extends Fragment implements View.OnClickListener {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private Button cate1,cate2,cate3,cate4,cate5,cate6,cate7,cate8;
    private SQLiteDatabase db;
    private Cursor cursor;


    public CategoryFragment() {
        // Required empty public constructor

    }
    public CategoryFragment(SQLiteDatabase db) {
        this.db = db;
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment CategoryFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static CategoryFragment newInstance(String param1, String param2) {
        CategoryFragment fragment = new CategoryFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_category, container, false);
        init(view);
        // 查询分类列表
        cursor = db.rawQuery("select id,name from category",null);
        List<Category> categoryList = new ArrayList<>();
        while (cursor.moveToNext()){
            Category category = new Category();
            String name = cursor.getString(cursor.getColumnIndex("name"));
            category.setName(name);
            categoryList.add(category);
        }
        renderCategoryButton(categoryList);

        // 给分类按钮绑定单击事件
        cate1.setOnClickListener(this);
        cate2.setOnClickListener(this);
        cate3.setOnClickListener(this);
        cate4.setOnClickListener(this);
        cate5.setOnClickListener(this);
        cate6.setOnClickListener(this);
        cate7.setOnClickListener(this);
        cate8.setOnClickListener(this);

        return view;
    }

    // 渲染分类按钮
    public void renderCategoryButton(List<Category> list){
        List<Button> categoryBtns = Arrays.asList(cate1, cate2, cate3, cate4, cate5, cate6, cate7, cate8);
        for (int i = 0; i < list.size(); i++) {
            categoryBtns.get(i).setText(list.get(i).getName());
        }
    }

    // 初始化组件
    public void init(View view){
        this.cate1 = view.findViewById(R.id.cate1);
        this.cate2 = view.findViewById(R.id.cate2);
        this.cate3 = view.findViewById(R.id.cate3);
        this.cate4 = view.findViewById(R.id.cate4);
        this.cate5 = view.findViewById(R.id.cate5);
        this.cate6 = view.findViewById(R.id.cate6);
        this.cate7 = view.findViewById(R.id.cate7);
        this.cate8 = view.findViewById(R.id.cate8);
    }

    @Override
    public void onClick(View v) {
        Button cateBtn = v.findViewById(v.getId());
        String cateName = cateBtn.getText().toString();
        // 意向跳转到分类小说列表页
        Intent intent = new Intent(getActivity(), NovelListActivity.class);
        intent.putExtra("cateName",cateName);
        startActivity(intent);
    }
}